var Crawler = require("crawler");
var url = require('url');
var fs = require('fs');
var request = require('request');
var uuid = require('node-uuid');
var path = require('path');

var utils = require('./utils');

function getRandom() {
    return ~~(Math.random() * 10)
}

function store(str, file) {
    fs.appendFile(file, str + '\n', function (err) {
        err && console.log(err)
    });
}

function isUrl(url) {
    return /(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])?/.test(url)
}

var header = {
    cookie: 'JSESSIONID=1BBCE4623B36652A127C26A754BA840B; Hm_lvt_038002b56790c097b74c818a80e3a68e=1439775796; __utma=18275762.624622171.1436842160.1440034729.1440119963.9; __utmz=18275762.1440119963.9.9.utmcsr=cas.ziroom.com|utmccn=(referral)|utmcmd=referral|utmcct=/CAS/login; Hm_lpvt_038002b56790c097b74c818a80e3a68e=1440641105; ziroom_userName=lijian; ziroom_token=a862088a9deb1b8f2f2a3d95bd88bf01; __utmc=18275762'
}

var host = 'http://ams.ziroom.com'
var pageNum = 1;
var listUrl = 'http://ams.ziroom.com/AMS/configuration/configurationProgram!listConfiguration.action'

var itemNum = 1;
var item1Url = '/AMS/hirerenewal/hireRenewal!matainRenewalContractInfo.action';
var item2Url = '/AMS/hire/hireContract!detailHireContract.action';

var item1HrefUrl = '/AMS/hirerenewal/hireRenewal!hireRenewalMatainFrame.action'
var item2HrefUrl = '/AMS/hire/hireContract!indexDetailHireContract.action'


function getListUrl() {
    return listUrl + '?pageNum=' + pageNum;
}

function getItemUrl(id, type) {
    if(type == 1)
        return host + item1Url + '?id=' +id + '&isPass=1&mayAudit=0&isUpdate=true&isAudit=&isSpecial=&isDisabled=1&isView=true'
    else
        return host + item2Url + '?id=' + id
}

function trim(str) {
    return str.replace(/(^\s*|(\s*$))/g, '');
}

var c = new Crawler({
    maxConnections : 10,
    //skipDuplicates: true,
    headers: header,
    // This will be called for each crawled page
    callback : function (error, result, $) {
        var url = result.uri;

        if(url.indexOf(listUrl)!=-1){
              console.log('页数：' + pageNum + '，已经爬完。')
              store('页数：' + pageNum,'./page.txt')
              pageNum++
                var random = getRandom()
                setTimeout(function() {
                    c.queue(getListUrl())
                }, random * 1000)
                console.log(random + 's后开始爬:第' + pageNum + '页')
                $('.grid-table tr').each(function(index, item){
                    var item = $(item);

                        var linkEle = item.find('td').eq(1).find('a');
                        var linkUrl = linkEle.attr('href')
                        var linkTxt = linkEle.text()

                        if(linkTxt && linkUrl){
                            var id = utils.paramOfUrl(linkUrl).id
                            //console.log(itemNum + ':id：' + id + ' 合同号：' + linkTxt + ' 链接：' + linkUrl)
                            //var url = getItemUrl(id)
                            var itemUrl = ''
                            if(linkUrl.indexOf(item1HrefUrl) != -1){
                                itemUrl = getItemUrl(id, 1)
                            }

                            if(linkUrl.indexOf(item2HrefUrl) != -1) {
                                itemUrl = getItemUrl(id, 2)
                            }

                            if(itemUrl){
                                setTimeout(function(){
                                    c.queue(itemUrl);
                                }, index * 300)
                            }

                        }
                })
        }else if(url.indexOf(item1Url)!=-1){
            //第一个页面
            //if(itemNum == 1)
                //console.log($.html())
               // console.log(url)
            //单个页面

            var result = ''
            var value = trim($('[id="hireContract.oldHireContract.hireContractCode"]').val())
            if(value){
                console.log('1条目：' + itemNum + '，已经查看');

                 //'原收房合同号：' +
                result += value + ',';
                // '原收房合同截止日期：' +
                value = trim($('[id="hireContract.oldHireContract.agentEndDate"]').val())
                result += value + ',';
                // '产权地址：' +
                value = trim($('[id="propertyAddress"]').val())
                result += value + ',';
                 //'续约合同签署日期：' +
                value = trim($('[id="hireContract.signDate"]').val())
                result += value + ',';
                // '房屋所有权人姓名：' +
                value = trim($('[id="hireContract.houseOwennerName"]').val())
                result += value + ',';
                //'出租人联系电话：' +
                value = trim($('[id="hireContract.owner.mobile"]').val())
                result +=  value + ',';
                //'代理人姓名：' +
                value = trim($('[id="hireContract.clientName"]').val())
                result +=  value + ',';
                // '代理人联系电话：' +
                value = trim($('[id="hireContract.clientPhone"]').val())
                result += value + ',';
                // '续约合同起租日期：' +
                value = trim($('[id="hireContract.agentStartDate"]').val())
                result += value + ',';
                // '续约收房月租金：' +
                value = trim($('[id="hireContract.monthlyRent"]').val())
                result +=value + ',';
                // '签约周期：' +
                value = trim($('[id="hireContract.hireYear"]').val()) + '年' + trim($('[id="hireContract.hireMonth"]').val()) + '月'
                result +=value + ',';
                //'给业主付款方式：' +
                value = trim($('[id="hireContract.paymentCycleCode"] [selected]').text())
                result +=  value + ',';
                //'续约合同截止日期：' +
                value = trim($('[id="hireContract.agentEndDate"]').val())
                result +=  value;


                console.log('1:' +result)
                store(result, './house1.txt')
                 itemNum++
            }else{
                c.queue(url);
            }


        }else if(url.indexOf(item2Url)!= -1){
            //第二个页面
           // if(itemNum == 1)
                //console.log($.html())
            //单个页面
           // console.log(url)
            console.log('2条目：' + itemNum + '，已经查看');
            var result = '';
            $('#myTable tr td').each(function(index, item){
                item = $(item);
                var key = trim(item.text());


                 if(key.indexOf('房屋名称') !=-1){
                    var value = trim(item.next().text());
                    result += value + ','
                }

                 if(key.indexOf('业主姓名') !=-1){
                    var value = trim(item.next().text());
                    result += value + ','
                }

                 if(key.indexOf('业主手机号') !=-1){
                    var value = trim(item.next().text());
                    result += value + ','
                }

                if(key.indexOf('出租合同号') !=-1){
                    var value = trim(item.next().text());
                    result += value + ','
                }

                if(key.indexOf('合同截至日期') !=-1){
                    var value = trim(item.next().text());
                    result += value + ','
                }

                 if(key.indexOf('收房价格') !=-1){
                    var value = trim(item.next().text());
                    result += value + ','
                }


            })


                console.log('2:' + result)
                store(result, './house2.txt')
                itemNum++


        }else{
            console.log('是不是没登陆啊')
        }


         /*var imgUrl = item.find('.Ii').attr('data-img');
            var uid = uuid.v4().replace(/-/g, '');

            var filepath = 'img/' + uid + '.jpg'//path.extname(imgUrl);

            request(imgUrl).pipe(fs.createWriteStream(filepath));
            store({
                system: {
                    url: result.uri,
                    time: new Date().strftime('%Y-%m-%d %H:%M:%S')
                },
                name: item.find('.Fl h4 i').text(),
                imgs: filepath
            })*/
        //console.log($.html())
        // $ is Cheerio by default
        //a lean implementation of core jQuery designed specifically for the server
        // $('.list-item').each(function(index, item){
        //     console.log($(item).find('.info1 i').text())
        // })
        // $('a').each(function(index, a) {
        //     var toQueueUrl = $(a).attr('href');
        //     if(/(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])?/.test(toQueueUrl)){
        //         console.log(toQueueUrl)
        //         c.queue(toQueueUrl);
        //     }
        // });
    }
});

c.queue(getListUrl())

//c.queue('http://www.iwjw.com/sale/shanghai/');
/*setInterval(function(){
    c.queue('http://www.iwjw.com/seeHouseList/')
}, 5*1000)*/

//需要先post请求
return false;
utils.ajax({
    host: 'http://ams.ziroom.com',
    fullUrl: 'http://ams.ziroom.com/AMS/configuration/configurationProgram!listConfiguration.action',
    method: 'post',
    data: {
        _pageNum: 1,
        _pageSize: 12,
        pageNum: 1,
        pageSize: 12
    },
    cookie: header.cookie,
    callback: function(data, body) {
        console.log(body)
    }
})
